export const Customer = {
  // 增加
  addCustomer: async (req, res) => {
    const {
      username,
      account,
      gender,
      password,
      address,
      phone,
      mail,
      remark,
    } = req.body;
    if (!username) return res.fail("用户名不能为空！");
    if (!account) return res.fail("账号不能为空");
    if (!password) return res.fail("密码不能为空");
    if (!gender) return res.fail("性别不能为空");
    if (!address) return res.fail("地址不能为空");
    if (!phone) return res.fail("电话不能为空");
    try {
      const newCustomer = {
        username,
        account,
        gender,
        password,
        address,
        phone,
      };
      if (mail) newCustomer.mail = mail;
      if (remark) newCustomer.remark = remark;
      await req.db.customer.create({
        data: newCustomer,
      });
      res.success("添加成功");
    } catch (e) {
      console.log("e", e);
      res.fail("添加异常");
    }
  },
  // 删除
  delCustomer: async (req, res) => {
    const { id } = req.body;
    if (!id) return res.fail("id不能为空");
    try {
      await req.db.customer.delete({
        where: {
          id: Number(id),
        },
      });
      res.success("删除成功");
    } catch (e) {
      console.log("e", e);
      res.fail("删除异常");
    }
  },
  //批量删除
  delSelectCustomer: async (req, res) => {
    const { ids } = req.body;
    if (!ids) return res.fail("ids not exit");
    try {
      await req.db.customer.deleteMany({
        where: {
          id: {
            in:ids.map(Number),
          },
        },
      });
      res.success("删除成功");
    } catch (e) {
      console.log("e", e);
      res.fail("删除异常");
    }
  },
  // 修改
  updateCustomer: async (req, res) => {
    const { id, ...other } = req.body;
    if (!id) return res.fail("id not required");
    try {
      const updateDate = {};
      delete req.body.id;
      for (let key in other) {
        if (!other[key]) continue;
        updateDate[key] = req.body[key];
      }

      await req.db.customer.update({
        where: {
          id: Number(id),
        },
        data: updateDate,
      });
      res.success("修改成功");
    } catch (e) {
      console.log("e", e);
      req.fail("更新失败");
    }
  },
  //单一查询
  getDail: async (req, res) => {
    const { id } = req.body;
    if (!id) return res.fail("id not exit");
    try {
      const one = await req.db.customer.findUnique({
        where: {
          id: Number(id),
        },
      });
      if (!one) return res.fail("该id不存在");
      res.success(one);
    } catch (e) {
      console.log("e", e);
      res.fail("获取失败");
    }
  },

  //分页查询
  getCustomerList: async (req, res) => {
    let { limit, page, a } = req.body;
    limit = Number(limit);
    page = Number(page);
    try {
      const where = {};
      if (a) where.a = { contains: a };
      const result = await req.db.customer.findMany({
        where,
        take: limit,
        skip: (page - 1) * limit,
      });
      const total = await req.db.customer.count({ where });
      res.success({ total, data: result });
    } catch (e) {
      console.log("e", e);
      res.fail("查询失败");
    }
  },
};
